#include "routeplanwid.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include "PlaceManger.h"
#include <QDebug>
#include <QMessageBox>
#include "mytool.h"

RoutePlanWid::RoutePlanWid(QWidget *parent) : QWidget(parent)
{
    this->setWindowTitle("线路规划");
    this->startLab = new QLabel("您出发的地点是：",this);
    this->endLab = new QLabel("你要去的地方是：",this);
    this->typeLab = new QLabel("请选择交通方式：");

    this->startBox = new QComboBox(this);
    this->endBox = new QComboBox(this);
    this->typeBox = new QComboBox(this);
    typeBox->setMinimumWidth(300);
    this->confirmBtn = new QPushButton("规划我的路线",this);
    this->confirmBtn->setMaximumWidth(200);

    QHBoxLayout *startLayout = new QHBoxLayout();
    QHBoxLayout *endLayout = new QHBoxLayout();
    QHBoxLayout *typeLayout = new QHBoxLayout();
    QHBoxLayout *btnLayout = new QHBoxLayout();
    QVBoxLayout *vlayout = new QVBoxLayout(this);

    startLayout->addWidget(startLab);
    startLayout->addWidget(startBox);
    endLayout->addWidget(endLab);
    endLayout->addWidget(endBox);
    typeLayout->addWidget(typeLab);
    typeLayout->addWidget(typeBox);
    btnLayout->addWidget(confirmBtn);

    vlayout->addLayout(startLayout);
    vlayout->addLayout(endLayout);
    vlayout->addLayout(typeLayout);
    vlayout->addLayout(btnLayout);
    vlayout->setMargin(40);
    vlayout->setSpacing(20);
    setLayout(vlayout);
    resize(400,300);

    this->comBoxInit();
    this->startBox->setMinimumWidth(300);
    this->endBox->setMinimumWidth(300);
    connect(confirmBtn,&QPushButton::released,this,&RoutePlanWid::confirm);
}

void RoutePlanWid::comBoxInit()
{
    this->startBox->clear();
    this->endBox->clear();
    this->typeBox->clear();
    PlaceManger PM;
    int sum = 0;
    Place *places = PM.List(sum);

    for(int i = 0;i<sum;i++)
    {
        if(strcmp(places[i].getName(),"")==0)
            continue;
        this->startBox->addItem(places[i].getName());
        this->endBox->addItem(places[i].getName());
    }
    this->typeBox->addItem("步行或单车");
    this->typeBox->addItem("校园公交");
    this->typeBox->addItem("最优游览方式");
}

void RoutePlanWid::confirm()
{
    char type[50];
    strcpy(type,this->typeBox->currentText().toStdString().c_str());

    char nameStart[50],nameEnd[50];
    strcpy(nameStart,this->startBox->currentText().toStdString().c_str());
    strcpy(nameEnd,this->endBox->currentText().toStdString().c_str());
    if(strcmp(nameStart,nameEnd)==0)
    {
        QMessageBox::information(this,"错误警告！","起点和终点不能相同!",QMessageBox::Yes);
        return ;
    }
    int x1,y1,x2,y2;
    PlaceManger PM;
    int sum = 0;
    Place *places = PM.List(sum);
    for(int i = 0;i<sum;i++)
    {
        if(strcmp(places[i].getName(),nameStart)==0)
        {
            x1 = places[i].getX();
            y1 = places[i].getY();
        }
        if(strcmp(places[i].getName(),nameEnd)==0)
        {
            x2 = places[i].getX();
            y2 = places[i].getY();
        }
    }

    if(strcmp(type,"步行或单车")==0)
    {
        qDebug() <<x1<<" "<<y1<<" "<<x2<<" "<<y2;
        emit RoutePlan(x1,y1,x2,y2);
    }
    else if(strcmp(type,"校园公交")==0)
    {
        int flag1 = 0;
        int flag2 = 0;
        MyTool tool;
        gjlx *path = tool.getAllGJnode();
        for(int i = 0;i<94;i++)
        {
            if(path[i].x==x1&&path[i].y==y1)
            {
                flag1 = 1;
            }
            if(path[i].x==x2&&path[i].y==y2)
            {
                flag2 = 1;
            }
        }
        if(!flag1)
        {
            QMessageBox::information(this,"warning","起点没有公交可达!",QMessageBox::Yes);
            return ;
        }
        if(!flag2)
        {
            QMessageBox::information(this,"warning","终点没有公交可达!",QMessageBox::Yes);
            return ;
        }
        emit RoutePlanGJ(2,x1,y1,x2,y2);
    }
    else
    {
        int flag1 = 0;
        int flag2 = 0;
        MyTool tool;
        gjlx *path = tool.getAllGJnode();
        for(int i = 0;i<94;i++)
        {
            if(path[i].x==x1&&path[i].y==y1)
            {
                flag1 = 1;
            }
            if(path[i].x==x2&&path[i].y==y2)
            {
                flag2 = 1;
            }
        }
        if(!flag1)
        {
            QMessageBox::information(this,"warning","起点没有公交可达!",QMessageBox::Yes);
            return ;
        }
        if(!flag2)
        {
            QMessageBox::information(this,"warning","终点没有公交可达!",QMessageBox::Yes);
            return ;
        }
        emit RoutePlanAll(x1,y1,x2,y2);
    }
}
